<?php

namespace app\model;

use think\Model;
use think\Db;
use app\model\BalanceModel;
class ChargeModel extends Model{


    protected $table = 'mm_charge';

    //创建缴费记录
    public function add($data){
        $data['account_id'] = $data['admin_auth']['auth_admin_id'];
        $data['cid'] = $data['admin_auth']['cid'];
        $data['account_name'] = $data['admin_auth']['nickname'];
        $data['students_sn'] = $data['order_sn'];
        unset($data['admin_auth']);
        unset($data['order_sn']);
        if(empty($data['agreement_price'])){
            return ['msg'=>'协议金额不能为空','code'=>0];
        }
        if(empty($data['submit_price'])){
            return ['msg'=>'已缴金额不能为空','code'=>0];
        }
        $data['overdue_price'] = $data['agreement_price'] - $data['submit_price'];
        $data['create_time'] = date('Y-m-d H:i:s',time());
        Db::startTrans();
        try {
            ChargeModel::insert($data);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return  ['msg'=>$e->getMessage(),'code'=>0];
        }
        return ['msg'=>'添加成功','code'=>1];
    }
    //创建缴费记录
    public function supplementAdd($data){
        //查询之前的补交记录
        $info = ChargeModel::where("students_sn",$data['order_sn'])->order("id",'desc')->find();
        $data['account_id'] = $data['admin_auth']['auth_admin_id'];
        $data['cid'] = $data['admin_auth']['cid'];
        $data['account_name'] = $data['admin_auth']['nickname'];
        $data['students_sn'] = $data['order_sn'];
        unset($data['admin_auth']);
        if(empty($data['supplement_price'])){
            return ['msg'=>'补交金额不能为空','code'=>0];
        }
        $data['agreement_price'] = $info->agreement_price;//协议金额
        $data['submit_price'] = $data['supplement_price'];//补交金额
        $data['overdue_price'] = abs($info->overdue_price) - abs($data['supplement_price']);//未交金额
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $res = ChargeModel::create($data);
       
        return $res ? ['msg'=>'添加成功','code'=>1] : ['msg'=>'添加失败','code'=>0];
    }
    //分公司审核
    public function financialAudit($data){
        if(empty($data['id'])){
            return ["code"=>0,'msg'=>'缺少必传数据'];
        }
        $info = ChargeModel::find($data['id']);
        if($info->status == 1){
            return ["code"=>0,'msg'=>'重复审核'];
        }elseif($info->status == 3){
            return ["code"=>0,'msg'=>'总公司已审核过'];
        }
        $info->audit_id = $data['admin_auth']['auth_admin_id'] ;
        $info->status = $data['status'] == 1 ? 1 : 4;
        $info->audit_name = $data['admin_auth']['nickname'] ;
        $info->update_time = date("Y-m-d H:i:s",time()) ;
        $res = $info->save();
        return $res ? ['msg'=>'审核通过','code'=>1] : ['msg'=>'审核失败','code'=>0];
    }
    //总公司审核
    public function financialTotalAudit($data){
        if(empty($data['id'])){
            return ["code"=>0,'msg'=>'缺少必传数据'];
        }
        $info = ChargeModel::find($data['id']);
        if($info->status == 2){
            return ["code"=>0,'msg'=>'请等待分公司财务审核'];
        }elseif($info->status == 3){
            return ["code"=>0,'msg'=>'重复审核'];
        }
        // $info->total_audit_id = $data['admin_auth']['auth_admin_id'] ;
        // $info->status = $data['status'] == 1 ? 3 : 5;
        // $info->total_audit_name = $data['admin_auth']['nickname'] ;
        // $info->total_update_time = date("Y-m-d H:i:s",time()) ;
        // $res = $info->save();

        Db::startTrans();
        try {
            ChargeModel::where("id",$info->id)->update([
                "total_audit_id" =>$data['admin_auth']['auth_admin_id'],
                "status" => $data['status'] == 1 ? 3 : 5,
                "total_audit_name" => $data['admin_auth']['nickname'],
                "total_update_time" =>date("Y-m-d H:i:s",time())
            ]);
            $Balance = BalanceModel::where("students_sn",$info->students_sn)->find();
            if($data['status'] == 1){//审核通过
                BalanceModel::where("students_sn",$info->students_sn)->update([
                        "agreement_price"=>$info->agreement_price,//协议金额
                        'submit_price'=> $info->submit_price + $Balance->submit_price,//已交金额 当前已交 + 之前已交
                        'overdue_price'=> $info->agreement_price-$info->submit_price-$Balance->overdue_price//未交金额= 协议金额 - 当前已交 - 之前未交
                ]);
            }else{//驳回
                BalanceModel::where("students_sn",$info->students_sn)->update([
                    "agreement_price"=>$info->agreement_price,//协议金额
                    'submit_price'=> $Balance->submit_price,//
                    'overdue_price'=> $info->agreement_price - $Balance->submit_price//
                ]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return  ['msg'=>$e->getMessage(),'code'=>0];
        }
        return ['msg'=>'审核通过','code'=>1] ;
    }
    //根据编号获取对应的缴费记录
    public static function getPrice($order_sn){
        $price = ChargeModel::where("students_sn",$order_sn)->order('id','desc')->find();
        if(empty($price)){//不存在缴费记录
            return 1;
        }else{
            if(abs($price->overdue_price) > 0){//补交
                return 2;
            }else{//已完成
                return 3;
            }
        }
    }
    //获取协议金额
    public static function agreementPrice($order_sn){
        $agreement_price = ChargeModel::where("students_sn",$order_sn)->order('id','desc')->value('agreement_price');
        return abs($agreement_price);
    }
    //获取未交金额
    public static function overduePrice($order_sn){
        $overdue_price = BalanceModel::where("students_sn",$order_sn)->value("overdue_price"); 
        $balance = BalanceModel::where("students_sn",$order_sn)->find(); 
        if($balance->agreement_price > 0){
            return abs($overdue_price);
        }
        return self::agreementPrice($order_sn);
    }

}